
package com.huaweicloud.haydn.delivercore.agent.config;

import com.huaweicloud.haydn.delivercore.agent.common.exception.BusinessErrorEnum;
import com.huaweicloud.haydn.delivercore.agent.common.exception.BusinessException;
import com.huaweicloud.haydn.delivercore.agent.service.metadata.DatabaseUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @since 2022-09-26
 */
@Service
public class DatabaseContext {

    @Autowired
    private final Map<String, DatabaseUtil> strategyMap = new ConcurrentHashMap<>();

    public DatabaseUtil getStrategy(String component) throws BusinessException {
        DatabaseUtil strategy = strategyMap.get(component);
        if (strategy == null) {
            throw new BusinessException(BusinessErrorEnum.BASE_INTERNAL_SERVER_ERROR, "no database strategy defined");
        } else {
            return strategy;
        }
    }
}
